<?php


namespace app\api\controller;


use think\facade\Request;

class User extends ApiBase
{
    public function createUser()
    {
        $user_name = input("user_name");
        $cookie_open_id = input("cookie_open_id");
        $data = [
            "user_name" => $user_name,
            "cookie_open_id" => $cookie_open_id
        ];
        $this->autoValid(\app\api\validate\User::class, $data);
        //找用户
        $whereUser = [];
        $whereUser[] = ["cookie_open_id", "=", $cookie_open_id];
        $user = \app\common\model\User::where($whereUser)->find();
        if ($user) {
            $user->user_name = $user_name;
            $user->update_time = $user->last_login_time = getNow();
        } else {
            $user = new \app\common\model\User($data);
            $user->headimg = Request::domain() . WEB_DIR . "/static/images/head/" . rand(1, 8) . ".jpg";
            $user->create_time = $user->update_time = $user->last_login_time = getNow();
        }
        $user->save();
        $userModel = array(
            'user_name' => $user->user_name,
            'id' => $user->id,
            'headimg' => $user->headimg,
            'token' => $user->token,
        );
        $this->success($userModel);

    }
}
